package com.dengdai.applibrary.widgets.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dengdai.applibrary.BaseApplication;
import com.dengdai.applibrary.config.ResIdName;
import com.dengdai.applibrary.config.ViewInfo;
import com.dengdai.applibrary.interfaces.PromptDialogInterface;
import com.dengdai.applibrary.utils.BaseViewUtils;

/**
 * @说明 显示提示对话框
 * @作者 LY
 * @时间 16/5/21 17:58
 * @邮箱 2743569843@qq.com
 * @版权 Copyright(c) 2016 LY-版权所有
 * @备注
 */
public class ShowPromptDialog extends Dialog {
    private TextView tvDialogTitle;
    private TextView tvDialogContent;
    private TextView btnDialogLeft;
    private LinearLayout dialogCuttingLine;
    private TextView btnDialogRight;

    public ShowPromptDialog(Context context) {
        super(context);
        initView();
    }

    /**
     * 显示对话框不带标题
     *
     * @param content               内容
     * @param btnLeft               左边按钮名称
     * @param btnRight              右边按钮名称
     * @param promptDialogInterface 事件监听
     * @param isCancelable          是否可以取消
     */
    public void showPromptNoTitle(String content, String btnLeft, String btnRight, final PromptDialogInterface promptDialogInterface, boolean isCancelable) {
        showPrompt(null, content, btnLeft, btnRight, promptDialogInterface, isCancelable);
    }

    /**
     * 显示对话框不带标题和单个按钮
     *
     * @param content               内容
     * @param btnRight              右边按钮名称
     * @param promptDialogInterface 事件监听
     * @param isCancelable          是否可以取消
     */
    public void showPromptNoTitleAndSingleBtn(String content, String btnRight, final PromptDialogInterface promptDialogInterface, boolean isCancelable) {
        showPrompt(null, content, null, btnRight, promptDialogInterface, isCancelable);
    }

    /**
     * 显示对话框单个按钮
     *
     * @param title                 标题
     * @param content               内容
     * @param btnRight              右边按钮名称
     * @param promptDialogInterface 事件监听
     * @param isCancelable          是否可以取消
     */
    public void showPromptSingleBtn(String title, String content, String btnRight, final PromptDialogInterface promptDialogInterface, boolean isCancelable) {
        showPrompt(title, content, null, btnRight, promptDialogInterface, isCancelable);
    }


    /**
     * 显示对话框
     *
     * @param title                 标题
     * @param content               内容
     * @param btnLeft               左边按钮名称
     * @param btnRight              右边按钮名称
     * @param promptDialogInterface 事件监听
     * @param isCancelable          是否可以取消
     */
    public void showPrompt(String title, String content, String btnLeft, String btnRight, final PromptDialogInterface promptDialogInterface, boolean isCancelable) {
        if (TextUtils.isEmpty(title)) {
            tvDialogTitle.setVisibility(View.GONE);
        } else {
            tvDialogTitle.setVisibility(View.VISIBLE);
            tvDialogTitle.setText(title);
        }
        if (TextUtils.isEmpty(content)) {
            tvDialogContent.setVisibility(View.GONE);
        } else {
            tvDialogContent.setVisibility(View.VISIBLE);
            tvDialogContent.setText(content);
        }
        if (TextUtils.isEmpty(btnLeft)) {
            btnDialogLeft.setVisibility(View.GONE);
            dialogCuttingLine.setVisibility(View.GONE);
        } else {
            btnDialogLeft.setVisibility(View.VISIBLE);
            dialogCuttingLine.setVisibility(View.VISIBLE);
            btnDialogLeft.setText(btnLeft);
        }
        if (TextUtils.isEmpty(btnRight)) {
            btnDialogRight.setVisibility(View.GONE);
        } else {
            btnDialogRight.setVisibility(View.VISIBLE);
            btnDialogRight.setText(btnRight);
        }
        btnDialogLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        btnDialogRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (promptDialogInterface != null) {
                    promptDialogInterface.determineClickInterface();
                }
                dismiss();
            }
        });
        setCancelable(isCancelable);
        show();
    }


    /**
     * 初始化界面
     */
    private void initView() {
        View view = LayoutInflater.from(BaseApplication.getAppContext()).inflate(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.LAYOUT, ViewInfo.SHOW_PROMPT_DIALOG), null);
        tvDialogTitle = (TextView) view.findViewById(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.ID, ResIdName.TVDIALOGTITLE));
        tvDialogContent = (TextView) view.findViewById(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.ID, ResIdName.TVDIALOGCONTENT));
        btnDialogLeft = (TextView) view.findViewById(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.ID, ResIdName.BTNDIALOGLEFT));
        btnDialogRight = (TextView) view.findViewById(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.ID, ResIdName.BTNDIALOGRIGHT));
        dialogCuttingLine = (LinearLayout) view.findViewById(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.ID, ResIdName.DIALOGCUTTINGLINE));
        //设置没有标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置背景颜色
        getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        addContentView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
    }
}
